﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KorleBu.Application_Classes;
using System.Drawing;

namespace KorleBu
{
    public partial class Requisitions1 : System.Web.UI.Page
    {
        #region Class State Variables
        DataSet ds = new DataSet();
        Presentation presentation = new Presentation();
 
        #endregion

        #region Session Methods

        private int GetStoreID()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getStoreId();
        }

        private int GetUserID()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getUserId();
        }
        private string GetSessionID()
        {
            try
            {
                User lg = (User)Session["LoggedInUser"];
                return lg.getSessionId();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                return null;
            }
        }
        private bool GetIsAdmin()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsAdmin();
        }
        private string GetFirstName()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getFirstName();
        }
        private string GetLastName()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getLastName();
        }
        private bool GetIsGeneralManager()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsGeneralManager();
        }
        private bool GetIsStoreManager()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsStoreManager();
        }
        private bool GetIsStoreKeeper()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsStoreKeeper();
        }
        private bool GetIsSubUnitClerk()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsSubUnitClerk();
        }
        private bool GetIsAccounting()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsAccounting();
        }
        private bool GetIsProcurement()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsProcurement();
        }
        private bool GetIsCEO()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsCEO();
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            //keep clean error message on load; other methods occur after load so they will fill in the error as needed.
            lblMasterError.Text = "";

            #region Aaron's Stuff
            //if the user didn't log in, but browsed to this page redirect them to the login page.
            if (GetSessionID() == null || GetSessionID() == "")
            {
                Response.Redirect("~/Login.aspx", true);
            }
            lblLoggedInUser.Text = "You are logged in as " + GetFirstName() + " " + GetLastName();
            #endregion

            #region redirects based on logins
            if (GetIsGeneralManager())
            {
                TabPanel1.Visible = false;
                ARRequisitions.Visible = false;
                ds = presentation.GetRequisitionsByPendingStatus();
                if (ds.Tables.Count > 0) // If the method returned successfully
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                    {
                        lblError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                    }
                    else
                    {
                        gvRequisition.DataSource = ds.Tables[1];
                        gvRequisition.DataBind();

                        // Color each item's status
                        foreach (GridViewRow gvr in gvRequisition.Rows)
                        {
                            Label lblStatusName = gvr.FindControl("lblStatusName") as Label;
                            String status = lblStatusName.Text;
                            if (status == "Pending") { lblStatusName.ForeColor = Color.DarkOrange; }
                            if (status == "Ready") { lblStatusName.ForeColor = Color.DarkGreen; }
                        }
                    }
                }
            }
            else if (GetIsCEO())
            {
                TabPanel1.Visible = false;
                ARRequisitions.Visible = false;
                ds = presentation.GetRequisitionsByGMStatus();
                if (ds.Tables.Count > 0) // If the method returned successfully
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                    {
                        lblError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                    }
                    else
                    {
                        gvRequisition.DataSource = ds.Tables[1];
                        gvRequisition.DataBind();

                        // Color each item's status
                        foreach (GridViewRow gvr in gvRequisition.Rows)
                        {
                            Label lblStatusName = gvr.FindControl("lblStatusName") as Label;
                            String status = lblStatusName.Text;
                            if (status == "Pending") { lblStatusName.ForeColor = Color.DarkOrange; }
                            if (status == "Ready") { lblStatusName.ForeColor = Color.DarkGreen; }
                        }
                    }
                }
            }
            else if (GetIsProcurement())
            {
                ARRequisitions.Visible = false;
                ViewRequisitions.Visible = false;

                ds = presentation.GetRequisitionsByCEOStatus();
                if (ds.Tables.Count > 0) // If the method returned successfully
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                    {
                        lblProcMessage.Text = "There are no pending requisitions";
                        lblError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                    }
                    else
                    {
                        gvProcRequisition.DataSource = ds.Tables[1];
                        gvProcRequisition.DataBind();
                        //lblSideBarContent.Text = "**You have pending requisition orders to handle.**";
                    }
                }
            }
            else
            {
                //lblSideBarContent.Text = "At any time, you can click on the logo above to return to this home page. Please click the \"logout\" button to end the program when you are finished working.";
            }

            #endregion


        }

        protected void gvRequisition_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                // Retrieve the row index stored in the 
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button 
                // from the Rows collection.
                GridViewRow row = gvRequisition.Rows[index];


                ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(row.Cells[0].Text), false);
                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    gvRequisitionItems.DataSource = ds.Tables[1];
                    lblRequisitionID.Text = row.Cells[0].Text;
                    lblRequisitionDate.Text = row.Cells[2].Text;
                    lblDescription.Text = row.Cells[1].Text;
                    lblStoreName.Text = row.Cells[3].Text;
                    DataSet reqInfo = presentation.GetRequisitionByRequisitionID(Convert.ToInt32(row.Cells[0].Text));
                    txtComments.Text = reqInfo.Tables[1].Rows[0]["DenialComments"].ToString();

                    ds = presentation.CheckSignature(Convert.ToInt32(lblRequisitionID.Text));

                    if (ds.Tables[0].Rows[0][0].ToString() == "")
                    {
                        lblGMSigned0.Text = ds.Tables[1].Rows[0][0].ToString();
                        lblCEOSigned0.Text = ds.Tables[1].Rows[0][1].ToString();
                    }
                    else
                        lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();

                gvRequisitionItems.DataBind();
                mvRequisition.ActiveViewIndex = 1;

                // Color each item's status
                foreach (GridViewRow gvr in gvRequisitionItems.Rows)
                {
                    Label lblStatusName = gvr.FindControl("lblStatusName") as Label;
                    String status = lblStatusName.Text;
                    if (status == "Pending") { lblStatusName.ForeColor = Color.DarkOrange; }
                    if (status == "Ready") { lblStatusName.ForeColor = Color.DarkGreen; }
                }
            }
        }

        protected void btnSelectReq_Click(object sender, EventArgs e)
        {
            mvRequisition.ActiveViewIndex = 0;
        }

        protected void gvRequisitionItems_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Deny")
            {
                // Retrieve the row index stored in the 
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button 
                // from the Rows collection.
                GridViewRow row = gvRequisitionItems.Rows[index];

                presentation.AcceptRejectRequisitionItem(Convert.ToInt32(lblRequisitionID.Text), Convert.ToInt32((row.FindControl("lblItemID") as Label).Text), true);
                ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblRequisitionID.Text), false);
                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    gvRequisitionItems.DataSource = ds.Tables[1];
                }
                else
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();

                gvRequisitionItems.DataBind();

                ds = presentation.CheckToCloseRequisition(Convert.ToInt32(lblRequisitionID.Text));

                if (ds.Tables[0].Rows[0][0].ToString() == "true")
                {
                    gvRequisitionItems.DataSource = null;
                    gvRequisitionItems.DataBind();
                    mvRequisition.ActiveViewIndex = 0;
                    Response.Redirect("Requisitions.aspx");
                }

                // Color each item's status
                foreach (GridViewRow gvr in gvRequisitionItems.Rows)
                {
                    Label lblStatusName = gvr.FindControl("lblStatusName") as Label;
                    String status = lblStatusName.Text;
                    if (status == "Pending") { lblStatusName.ForeColor = Color.DarkOrange; }
                    if (status == "Ready") { lblStatusName.ForeColor = Color.DarkGreen; }
                }
            }
        }

        protected void btnSaveComments_Click(object sender, EventArgs e)
        {
            presentation.AddCommentToRequsition(Convert.ToInt32(lblRequisitionID.Text), txtComments.Text);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            presentation.AcceptRequisition(Convert.ToInt32(lblRequisitionID.Text), GetUserID(), GetIsGeneralManager(), GetIsCEO());
            mvRequisition.ActiveViewIndex = 0;
            Response.Redirect("Requisitions.aspx");
        }

        protected void gvProcRequisition_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Choose")
            {
                // Retrieve the row index stored in the 
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button 
                // from the Rows collection.
                GridViewRow row = gvProcRequisition.Rows[index];

                ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(row.Cells[0].Text), false);
                if (ds.Tables[0].Rows[0][0].ToString() == "")
                {
                    gvProcItem.DataSource = ds.Tables[1];
                    lblProcRequisitionID.Text = row.Cells[0].Text;
                    lblProcDate.Text = row.Cells[2].Text;
                    lblProcDescription.Text = row.Cells[1].Text;
                }
                else
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();

                gvProcItem.DataBind();
                mvProc.ActiveViewIndex = 1;
            }
        }

        protected void btnSelectNewProc_Click(object sender, EventArgs e)
        {
            ds = presentation.GetRequisitionsByCEOStatus();
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblProcMessage.Text = "There are no pending requisitions";
                    lblError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                    gvProcRequisition.DataSource = null;
                    gvProcRequisition.DataBind();
                }
                else
                {
                    gvProcRequisition.DataSource = ds.Tables[1];
                    gvProcRequisition.DataBind();
                    //lblSideBarContent.Text = "**You have pending requisition orders to handle.**";
                }
            }
            mvProc.ActiveViewIndex = 0;
        }

        protected void gvProcItem_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Remove")
            {
                // Retrieve the row index stored in the 
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button 
                // from the Rows collection.
                GridViewRow row = gvProcItem.Rows[index];

                presentation.AcceptRejectRequisitionItem(Convert.ToInt32(lblProcRequisitionID.Text), Convert.ToInt32((row.FindControl("lblItemID") as Label).Text), true);
                ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblProcRequisitionID.Text), false);
                
                if (ds.Tables[0].Rows[0][0].ToString() == "")
                    gvProcItem.DataSource = ds.Tables[1];
                else
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
    
                gvProcItem.DataBind();

                ds = presentation.CheckToCloseRequisition(Convert.ToInt32(lblProcRequisitionID.Text));

                if (ds.Tables[0].Rows[0][0].ToString() == "true")
                {
                    gvRequisitionItems.DataSource = null;
                    gvRequisitionItems.DataBind();
                    mvRequisition.ActiveViewIndex = 0;
                    Response.Redirect("Requisitions.aspx");

                    // Color each item's status
                    foreach (GridViewRow gvr in gvRequisitionItems.Rows)
                    {
                        Label lblStatusName = gvr.FindControl("lblStatusName") as Label;
                        String status = lblStatusName.Text;
                        if (status == "Pending") { lblStatusName.ForeColor = Color.DarkOrange; }
                        if (status == "Ready") { lblStatusName.ForeColor = Color.DarkGreen; }
                    }
                }


            }

            if (e.CommandName == "Order")
            {
                // Retrieve the row index stored in the 
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button 
                // from the Rows collection.
                GridViewRow row = gvProcItem.Rows[index];

                presentation.MarkItemOrdered(Convert.ToInt32(lblProcRequisitionID.Text), Convert.ToInt32((row.FindControl("lblItemID") as Label).Text));

                ds = presentation.GetRequisitionItemsByRequisitionID(Convert.ToInt32(lblProcRequisitionID.Text), false);
                if (ds.Tables[0].Rows[0][0].ToString() == "")
                    gvProcItem.DataSource = ds.Tables[1];
                else
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                gvProcItem.DataBind();
            }
        }

        protected void gvProcItem_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells[2].Text == "Ordered")
            {
                e.Row.Cells[3].Enabled = false;
                e.Row.Cells[4].Enabled = false;
            }
        }

        public String CalcCost(Object qty, Object cost)
        {
            double dQty = Convert.ToDouble(qty);
            double dCost = Convert.ToDouble(cost);
            double total = dQty * dCost;

            return String.Format("{0:0.00}", total);
        }
    }
}